import Vue from 'vue'
import Vuex from 'vuex'
import orderStore from './modules/orderStore'
import goodsStore from './modules/goodsStore'
import staffStore from './modules/staffStore'
import supplierStore from './modules/supplierStore'
import userStore from './modules/userStore'
import addGoodsStore from './modules/addGoodsStore'
import http from '../ajax/http'
import * as url from '../ajax/api'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user:{
      //项目加载时，如果没有值，初始化为undefined
      userName:localStorage.getItem('userName')||undefined,
      token:localStorage.getItem('token')||undefined,
      type:localStorage.getItem('type')||undefined,
    }
  },
  mutations: {
    addUserLogin(state,obj){
      state.user.code= obj.code
      state.user.userName = obj.name
      state.user.token = obj.token
      state.user.data = obj.data
      state.user.type = obj.type
    },
    removeUser(state){
      state.user.code= undefined
      state.user.userName = undefined
      state.user.token = undefined
      state.user.data = undefined
      state.user.type = undefined
    }
  },
  actions: {
    uesrLogin({commit},obj){
      //console.log(obj)
      return http({
        url:url.USERLOGIN,
        method:'post',
        data:obj,
      }).then(res=>{
        //console.log(res.data)
        commit('addUserLogin',res.data)
        return res.data.code
      })
    },
    outLogin({commit}){
      localStorage.removeItem('user')
      localStorage.removeItem('token')
      localStorage.removeItem('type')
      commit('removeUser')
    }
  },
  getters:{
    getType(state){
      if(state.user.type === 'boss'){
        return '总经理'
      }else if(state.user.type === 'leader'){
        return '项目经理'
      }else if(state.user.type === 'staff'){
        return '普通员工'
      }
    }
  },
  modules: {
    orderStore,
    goodsStore,
    staffStore,
    supplierStore,
    userStore,
    addGoodsStore,
  }
})

